var sliderWidth = 15; // 需要设置slider的宽度，用于计算中间位置
var api = require('../../utils/api.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["直播", "视频", "需求"],
    activeIndex: 0,
    recordindex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    apptoken: '',
    keywords:'',
    lists:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    try {
      var value = wx.getStorageSync('token')
      if (value) {
        that.setData({
          apptoken: value
        })
      }
    } catch (e) {
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },
  /**
   * 切换搜索条件
  */
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    this.searchAction();
  },
  OnKeywords(e) {
    var that = this;
    that.setData({
      keywords:e.detail.value
    })
    that.searchAction();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //搜索
  searchAction: function(e) {
    var that = this;
    var index = that.data.activeIndex;
    that.setData({
      recordindex: index
    })
    api.post({
      url: 'Home/search',
      data:{
        'token':that.data.apptoken,
        'type': that.data.activeIndex,
        'keywords':that.data.keywords
      },
      success: data => {
        if (data.code == 200) {
          switch (Number(that.data.activeIndex)) {
            case 0:
              that.setData({
                contentArray: data.data.lists
              })
              break;
            case 1:
              that.setData({
                contentArray: data.data.lists
              })
              break;
            case 2:
              that.setData({
                contentArray: data.data.lists
              })
              break;
          }
        } else if (data.code == 300) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        } else if (data.code == 400) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        }
      }
    })
  },

  //提交搜索表单
  formSubmit: function (e) { 
    var that = this;
    var index = that.data.activeIndex;
    that.setData({
      recordindex: index
    })
    e.detail.value.token = that.data.apptoken;
    api.post({
      url: 'Home/search',
      data: e.detail.value,
      success: data => {
        if (data.code == 200) {
          switch (Number(that.data.activeIndex)) {
            case 0:
              that.setData({
                contentArray: data.data.lists
              })
              break;
            case 1:
              that.setData({
                contentArray: data.data.lists
              })
              break;
            case 2:
              that.setData({
                contentArray: data.data.lists
              })
              break;
          }
        } else if (data.code == 300) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        } else if (data.code == 400) {
          wx.showToast({
            title: data.data.message,
            icon: 'none'
          })
        }
      }
    })
  }
})